<?php

/**
 * MeetModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * : tool.php 6 2012-07-28 15:01:50Z rendong237@gmail.com $
 */
class MeetModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		$v["mid"] = (int)$value["mid"];
		$v["uid"] = (int)$value["uid"];
		$v["name"] = $value["name"];
		$v["enname"] = $value["enname"];
		$v["domain"] = $value["domain"];
		$v["unit"] = $value["unit"];
		$v["enunit"] = $value["enunit"];
		$v["language"] = (int)$value["language"];
		$v["startdate"] = $value["startdate"];
		$v["enddate"] = $value["enddate"];
		$v["address"] = $value["address"];
		$v["enaddress"] = $value["enaddress"];
		$v["joinnum"] = (int)$value["joinnum"];
		$v["regstart"] = $value["regstart"];
		$v["regend"] = $value["regend"];
		$v["isinvite"] = (int)$value["isinvite"];
		$v["style"] = (int)$value["style"];
		$v["image"] = $value["image"];
		$v["top"] = array();
		$v["left"] = $value["left"];
		$v["homeuuid"] = $value["homeuuid"];
		$v["regitems"] = "";
		$v["status"] = (int)$value["status"];
		$v["public"] = (int)$value["public"];
		$v["del"] = (int)$value["del"];
		$v["email"] = $value["email"];
		$v["emailuser"] = $value["emailuser"];
		$v["emailpwd"] = $value["emailpwd"];
		$v["smtpserver"] = $value["smtpserver"];
		$v["createtime"] = $value["createtime"];
		$v["submittime"] = $value["submittime"];
		$v["checktime"] = $value["checktime"];
		
		$v["top"][] = array( "uuid" => md5(uniqid(mt_rand(), true)), "type"=>"login", "name" => '注册登录', "aid" => 0,"enname" => 'Registration&Login', "enaid" => 0, "show" => true);
		$v["top"][] = array( "uuid" => md5(uniqid(mt_rand(), true)), "type"=>"file", "name" => '上传文件', "aid" => 0,"enname" => 'upload', "enaid" => 0, "show" => true);
		$v["top"][] = array( "uuid" => md5(uniqid(mt_rand(), true)), "type"=>"article", "name" => '会议介绍', "aid" => 0,"enname" => 'about', "enaid" => 0, "show" => true);
		
		$v["top"] = serialize($v["top"]);
		
		$mid = $this->db->insert("meet", $v, true);
		
		return $mid;
	}
	
	/**
	 * 修改
	 */
	public function change($mid, array $value)
	{
		$v = array();
		isset($value["uid"]) && $v["uid"] = (int)$value["uid"];
		isset($value["name"]) && $v["name"] = $value["name"];
		isset($value["enname"]) && $v["enname"] = $value["enname"];
		isset($value["domain"]) && $v["domain"] = $value["domain"];
		isset($value["unit"]) && $v["unit"] = $value["unit"];
		isset($value["enunit"]) && $v["enunit"] = $value["enunit"];
		isset($value["language"]) && $v["language"] = (int)$value["language"];
		isset($value["startdate"]) && $v["startdate"] = $value["startdate"];
		isset($value["enddate"]) && $v["enddate"] = $value["enddate"];
		isset($value["address"]) && $v["address"] = $value["address"];
		isset($value["enaddress"]) && $v["enaddress"] = $value["enaddress"];
		isset($value["joinnum"]) && $v["joinnum"] = (int)$value["joinnum"];
		isset($value["regstart"]) && $v["regstart"] = $value["regstart"];
		isset($value["regend"]) && $v["regend"] = $value["regend"];
		isset($value["isinvite"]) && $v["isinvite"] = (int)$value["isinvite"];
		isset($value["style"]) && $v["style"] = (int)$value["style"];
		isset($value["image"]) && $v["image"] = $value["image"];
		isset($value["top"]) && $v["top"] = $value["top"];
		isset($value["left"]) && $v["left"] = $value["left"];
		isset($value["homeuuid"]) && $v["homeuuid"] = $value["homeuuid"];
		isset($value["regitems"]) && $v["regitems"] = $value["regitems"];
		isset($value["status"]) && $v["status"] = (int)$value["status"];
		isset($value["public"]) && $v["public"] = (int)$value["public"];
		isset($value["del"]) && $v["del"] = (int)$value["del"];
		isset($value["email"]) && $v["email"] = $value["email"];
		isset($value["emailuser"]) && $v["emailuser"] = $value["emailuser"];
		isset($value["emailpwd"]) && $v["emailpwd"] = $value["emailpwd"];
		isset($value["smtpserver"]) && $v["smtpserver"] = $value["smtpserver"];
		isset($value["createtime"]) && $v["createtime"] = $value["createtime"];
		isset($value["submittime"]) && $v["submittime"] = $value["submittime"];
		isset($value["checktime"]) && $v["checktime"] = $value["checktime"];
		isset($value["newsletterkey"]) && $v["newsletterkey"] = $value["newsletterkey"];
		isset($value["newslettervalue"]) && $v["newslettervalue"] = $value["newslettervalue"];

		$this->db->update("meet", $v, 'mid=' . (int)$mid);
		
	}
	
	/**
	 * 删除
	 */
	public function del($mid)
	{
		// 逻辑删除，不物理删除
		$newValues = array("del" => 1, "public" => 0, "status" => 0);
		$this->db->update("meet", $newValues, "mid=" . (int)$mid);
		//$this->db->delete("meet", 'mid=' .  (int)$mid);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($nowp, $per_page)
	{
		$where = '';		
		$totalNum = $this->db->fetchOne ('select count(*) from meet ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from meet' . $where .' order by mid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	
	/**
	 * 首页读取
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchIndexAll($query, $startdate, $enddate, $nowp, $per_page)
	{
		$where = " where startdate>'$startdate' and startdate<'$enddate' and status=3 and public=1";
		if (!empty($query)){
			$query = str_replace(array('%', '?', '"', '\''), '', $query);
			$query = mysql_real_escape_string($query);
			
			$where = " where name like '%{$query}%' and status=3 and public=1";
		}
		
		$totalNum = $this->db->fetchOne ('select count(*) from meet ' . $where);
	
		require_once APP . '/application/models/Util.php';
	
		$pageInfo = Util::page ($totalNum, $per_page, $nowp);
		$start    = $pageInfo ['start'];
		$list     = $this->db->fetchAll ('select * from meet' . $where .' order by startdate desc limit ' . $start . ',' . $per_page);

		return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	
	/**
	 * 根据状态 读取列表
	 * 
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAllByStatus($status, $nowp, $per_page)
	{
		$where = ' where status=' . $status . ' and del=0';
		$totalNum = $this->db->fetchOne ('select count(*) from meet ' . $where);
	
		require_once APP . '/application/models/Util.php';
	
		$orderby = " order by submittime desc ";
		if ($status == 3)
			$orderby = " order by checktime desc ";
		
		$pageInfo = Util::page ($totalNum, $per_page, $nowp);
		$start    = $pageInfo ['start'];
		$list     = $this->db->fetchAll ('select t1.*,t2.name as uname, t2.email as uemail from meet t1 left join user t2 on t2.uid=t1.uid' . $where .' ' . $orderby . ' limit ' . $start . ',' . $per_page);
	
		return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	
	public function fetchAllByExpire($nowp, $per_page)
	{
		$where = ' where (status=0 or status=2) and del=0 and startdate<"' . date("Y-m-d H:i:s") . '"';
		$totalNum = $this->db->fetchOne ('select count(*) from meet ' . $where);
	
		require_once APP . '/application/models/Util.php';
	
		$orderby = " order by submittime desc ";
	
		$pageInfo = Util::page ($totalNum, $per_page, $nowp);
		$start    = $pageInfo ['start'];
		$list     = $this->db->fetchAll ('select t1.*,t2.name as uname from meet t1 left join user t2 on t2.uid=t1.uid' . $where .' ' . $orderby . ' limit ' . $start . ',' . $per_page);
	
		return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	
	/**
	 * 读取用户的会议列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAllByUid($uid, $nowp, $per_page)
	{
		$where = ' where uid=' . $uid . ' and del=0';
		$totalNum = $this->db->fetchOne ('select count(*) from meet ' . $where);
	
		require_once APP . '/application/models/Util.php';
	
		$pageInfo = Util::page ($totalNum, $per_page, $nowp);
		$start    = $pageInfo ['start'];
		$list     = $this->db->fetchAll ('select * from meet' . $where .' order by mid desc limit ' . $start . ',' . $per_page);
	
		return Array ('page' => $pageInfo['page'], 'list' => $list);
	}

	public function isExistDomain($domain)
	{
		$arr_system = array("admincss", 'adminimages', "adminjs", "application", "cache",
				"doc", "frame", "images", "js", "style", "upload");
		
		if (in_array($domain, $arr_system))
			return true;
		
		return $this->db->fetchOne("select count(*) from meet where domain='?' and del=0", array($domain));
	}
	
	/**
	 * 显示
	 */
	public function show($mid)
	{
		$content = $this->db->fetchRow("select * from `meet` where mid=\"?\"", array($mid));
		if (!empty($content)){
			$content['startdate'] = substr($content['startdate'], 0, 10);
			$content['enddate'] = substr($content['enddate'], 0, 10);
			$content['regstart'] = substr($content['regstart'], 0, 10);
			$content['regend'] = substr($content['regend'], 0, 10);
			
			$arr = array("c_blue", "c_ora", "c_green", "c_purple");
			$content['c_style'] = $arr[$content['style']];
		}
		
		return $content;
	}
	
	/**
	 * 显示
	 */
	public function showByDomain($domain)
	{
		return $this->db->fetchRow("select * from `meet` where domain=\"?\"", array($domain));
	}
}